<?php

class User extends CI_Controller {

    function __construct() {
        parent::__construct();
    }

    function index() {
        $level_id = $this->session->userdata('logged_in');

        if ($level_id['level_id'] == null || $level_id['level_id'] != 0) {
            redirect('user/login', 'refresh');
        }

        $crud = new grocery_CRUD();

        $crud->set_table('user');
        $crud->set_relation('level_id', 'level', 'level_name');	
		$crud->order_by('disabled', 'desc');
        $crud->required_fields('emp_name', 'level_id', 'username', 'password');
        $crud->display_as('emp_name', 'Employee Name');
        $crud->display_as('level_id', 'Acces Right');
        $crud->display_as('username', 'Username');
        $crud->display_as('password', 'Password');
        $crud->set_subject('User');
        $crud->field_type('password', 'password');
		$crud->unset_fields('last_modified');
		
		$crud->callback_insert(array($this, 'cb_insert_user'));
		$crud->callback_update(array($this, 'cb_update_user'));

        $crud->unset_read();

        $output = (array) $crud->render();
        $output['title'] = 'User List';

        $this->load->template('user/index', $output);
    }
	
	function cb_insert_user($post) {
		$post['password'] = md5($post['password']);
		
		return $this->db->insert('user', $post);
	}
	
	function cb_update_user($post, $pk) {
		$post['password'] = md5($post['password']);
		
		return $this->db->where('user_id', $pk)->update('user', $post);
	}

    function login() {
        $level_id = $this->session->userdata('logged_in');

        if ($level_id['level_id'] != null && $level_id['level_id'] == 0) {
            redirect('user', 'refresh');
        }

        if ($level_id['level_id'] != null && $level_id['level_id'] == 1) {
            redirect('reservation', 'refresh');
        }

        if ($level_id['level_id'] != null && $level_id['level_id'] == 2) {
            redirect('reservation', 'refresh');
        }

        $data['status'] = '';
        $this->load->view('user/login', $data);
    }

    function verify() {
        $this->load->model('user_model');

		$user = quotes_to_entities($_POST['username']);
		$pwd = md5(quotes_to_entities($_POST['password']));
        //query the database
        $result = $this->user_model->login($user, $pwd);
        if ($result) {
            $sess_array = array();
            foreach ($result as $row) {
                $sess_array = array(
                    'user_id' => $row->user_id,
                    'username' => $row->username,
                    'level_id' => $row->level_id,
					'emp_name' => $row->emp_name,
                    'staff_name' => $row->emp_name,
					'level_name' => $row->level_name
                );
                $this->session->set_userdata('logged_in', $sess_array);
            }
            if ($row->level_id == 0)
                redirect('user', 'refresh');
            else
                redirect('reservation', 'refresh');
            
        } else {
            $data['status'] = 'error';
            $this->load->view('user/login', $data);
        }
    }

    function logout() {
        $this->session->unset_userdata('logged_in');
        redirect('user/login', 'refresh');
    }

}
